<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>应急信息发布区域管理</title>
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <link href="../../layui/css/layui.css" rel="stylesheet" type="text/css">
    <link href="../../css/style.css" rel="stylesheet" type="text/css">
    <script src="../../layui/layui.js"></script>
    <script src="../../js/jquery-1.8.3.js"></script>
    <script src="../../js/util.js"></script>
    <script type="text/javascript">
        layui.use(['table', 'form', 'jquery','element','layer'], function () {
            var table = layui.table;
            var form = layui.form;
            var layer=layui.layer;
            var $ = layui.$;
            var mgrId="";
            var cityId="";
            var regionTypeValue = "";
            function appendCountyCheckBox(data){
                if (data.length != null) {
                    var checkboxs = "";
                    for (var i = 0; i < data.length; i++) {
                        checkboxs += '<input type="checkbox" value="' + data[i].countyId + '" name="countyId"  title="' + data[i].countyName + '">';
                    }
                    $('#quxiancheckbox').html(checkboxs);
                } else {
                    $('#quxiancheckbox').html('该地市下没有区县');
                }
                var boxes = document.getElementsByName("countyId");
                        if (regionTypeValue != null && regionTypeValue!='') {
                            var list = regionTypeValue.split(",");
                            for (i = 0; i < boxes.length; i++) {
                                for (j = 0; j < list.length; j++) {
                                    if (boxes[i].value == list[j]) {
                                        boxes[i].checked = true;
                                        break;
                                    }
                                }
                            }
                        }
                form.render();
            }
            $(document).ready(function () {
                 cityId=getUrlParam("cityId");
                 mgrId=getUrlParam("mgrId");
                 regionTypeValue=getUrlParam("regionTypeValue");
                 var data1=selectCountyId(cityId);
                 appendCountyCheckBox(data1);
            });
            //根据城市ID查询区县
            function selectCountyId(cityId) {
                var data;
                var url = '/util/findJFCountyByCityId?cityId=' + cityId;
                $.ajax({
                        type: 'post',
                        async: false,
                        url: url,
                        success: function (json) {
                            data = json.data;
                        }, error: function () {
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
			//获取行政区域内的基站的信息并插入基站表
            $('#countySave').on('click', function () {
            	layer.confirm('此操作将保存发送范围，请确认', {
                    btn: ['确认提交', '再看看']
                }, function () {
                	countySave();
                }, function() {
    				reset = 0;
                })
            });
            function countySave(){
            	var cityId=getUrlParam("cityId");
		   		var mgrId=getUrlParam("mgrId");
		   		var sendStart=getUrlParam("sendStart");
		   		var sendEnd=getUrlParam("sendEnd");
		   		var scopeType=getUrlParam("scopeType");
		   		var countyId=getUrlParam("countyId");
		   		var mgrState=getUrlParam("mgrState");
		   		var checkState=getUrlParam("checkState");
                var quxian="";
                var stationIds="";
                $("input:checkbox[name='countyId']:checked").each(function () {
                     quxian += $(this).val() + ",";
                });
                if(cityId==""){
                    layer.msg('未获取地市信息，请重新选取');
				    return false;
				}
                if(mgrId==""){
                    layer.msg('未获取项目ID，请重新选取');
				    return false;
				}
				if(quxian==""){
                    layer.msg('请重新选取行政区域组合');
				    return false;
				}
				quxian=quxian.substring(0,quxian.length-1);
                var index = layer.load(1, {shade: [0.1,'#fff'] });//0.1透明度的白色背景
                var param2 = {
                    mgrId:mgrId,
                    sendStart: sendStart,   //项目开始时间
                    sendEnd: sendEnd,//结束时间
                    scopeType: scopeType,//发送范围类型
                    cityId: cityId,
                    countyId: quxian,
                    checkState: checkState,//0审核通过 ，1审核不通过，2未审核
                    mgrState: mgrState//0开启 ，1暂停， 2关闭
                };
		        var jsonParams=JSON.stringify(param2);
		        //alert("jsonParams:"+jsonParams);
		        var url = "/smsInterfaces/sendMgrLimit";
                var url2 = '/smsArea/mapSave';
                var data;
                var flag=getUrlParam("flag");
                $.ajax({
                		contentType:"application/json;charset=UTF-8",
                        url: url,
                        data: jsonParams,
                        type:"post",
                        success: function (json) {
                        	if(json.retCode==0){
                        		stationIds=json.stationIds;
                        	  var markers = json.stationIds.split(',')
						      var resultStation=[];
						      for(var i = 0;i < markers.length; i++){
						        	var object2 = new Object();
						            object2.stationid=markers[i];
						            resultStation.push(object2);
						      }
						      
                        		var param = {
							        cityId: cityId,
							        mgrId: mgrId,
							        /* countyId:quxian,
							        stationIds:stationIds, */
							        resultRegionType:'1',
							        resultRegionValue:quxian,
							        resultStation:JSON.stringify(resultStation)
							    };
                        		$.ajax({
                        			 url: url2,
			                         data: param,
			                         type:"post",
			                         success: function (json) {
			                         	data = json.data;
			                            layer.close(index);
			                            if(json.code == 0&&data[0]>0){
				                            layer.confirm('成功保存发送区域,共'+data[1]+'个基站', {
				                                 btn: ['确定'] //按钮
				                            }, function () {
				                            if(flag=="insert"){
												parent.parent.location.href="../index.html";
											}else{
												layer.close(layer.index);
				                                 jumpToFisrt(mgrId,data[2],data[3],data[4]);
											}
				                            });
			                            }else{
			                                if(data[0]==-99){
			                                    layer.msg('该行政区域下无基站，请重新选取');
			                                }else{
			                                    layer.msg('保存发送区域失败，请重试');
			                                }
			                            } 
			                         },error:function(){
			                         	layer.close(index);
			                         	layer.msg('系统异常，请联系管理员处理!');
			                         }
                        		});
                        	}else{
                        		layer.close(index);
                        		layer.msg('获取基站信息失败，请重试');
                        	}
                        }, error: function () {
                        	layer.close(index);
                            layer.msg('系统异常，请联系管理员处理!');
                        }
                    }
                );
                return data;
            }
            function jumpToFisrt(mgrId,regionid,type,detail){
			    parent.$('#xiangmuid').val(mgrId);
			    parent.$('#quyuid').val(regionid);
			    parent.$('#quyutype').val(type);
			    parent.$('#quyudetail').val(detail);
			    parent.$('#xiangmuvalue').html("已选取");
			    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
			    parent.layer.close(index); //再执行关闭
			}
        });
    </script>
 
</head>
<body>
<div class="content-box">
<div class="layui-form layui-form-pane">
<div id="quxian" >
    <div >
        <div  id="quxiancheckbox"></div>
    </div>
</div> 
<div class="layui-form-item">
    <button class="layui-btn" id="countySave" >保存</button>
</div>
</div>
</div>
</body>
</html>